package com.boarsoft.message.activemq;

import javax.jms.Message;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.message.core.MessageService;
import com.boarsoft.message.jms.JmsMsgConverter;
import com.boarsoft.soagov.spy.SvcSpy;

public class AmqConsumerEx extends AmqConsumer {
	private static Logger log = LoggerFactory.getLogger(AmqConsumerEx.class);

	/** */
	protected MessageService handler;
	/** */
	protected JmsMsgConverter converter;
	/** */
	protected SvcSpy svcSpy;

	@Override
	public void onMessage(Message msg) {
		if (handler != null && converter != null) {
			try {
				com.boarsoft.message.bean.Message m = converter.convert(msg);
				//TODO 暂时放这里
				if (svcSpy != null && svcSpy.check(m) != SvcSpy.CHECK_PASS) {
					return;
				}
				handler.put(m);
			} catch (Exception e) {
				log.error("Error on handle jms message", e);
			}
		}
		if (listener != null) {
			listener.onMessage(msg);
		}
	}

	public com.boarsoft.message.bean.Message get() throws Exception {
		Message msg = consumer.receive();
		return converter.convert(msg);
	}

	public MessageService getHandler() {
		return handler;
	}

	public void setHandler(MessageService handler) {
		this.handler = handler;
	}

	public JmsMsgConverter getConverter() {
		return converter;
	}

	public void setConverter(JmsMsgConverter converter) {
		this.converter = converter;
	}

	public SvcSpy getSvcSpy() {
		return svcSpy;
	}

	public void setSvcSpy(SvcSpy svcSpy) {
		this.svcSpy = svcSpy;
	}
	
}
